<?php

namespace backend\controllers;

use bc\controller\BackendController;
use bc\model\BindModel;
use bc\model\OrderbindMode;
use bc\util\Concurrency;
use bc\util\Sms;

class TestController extends BackendController
{
	private $allActions;
	
	public function actionUpload()
	{
		return $this->render('upload');
	}
	
	/**
	 * 
	 */
	public function actionTest()
	{
		if($_FILES['file']['error'] == 0)
		{
			
			$inputFileName = $_FILES['file']['tmp_name'];
			$objPHPExcel = \PHPExcel_IOFactory::load($inputFileName);
			$sheetData = $objPHPExcel->setActiveSheetIndex(0)->toArray();
			
			foreach ($sheetData as $key=>$item)
			{
				if($key > 0)
				{
					$sms = new Sms();
					$sms->setMobile($item[1]);
					$sms->setText($item[2]);
					$sms->send();
				}
			}
			
			echo 'done';
		}
		
	}
	

	
	public function actionExport()
	{
		$objPHPExcel = new \PHPExcel();
		
		$filename = '导出测试';
		
		$data[]['name'] = '310106198712180832';
		
		// 表头
		$objPHPExcel->setActiveSheetIndex(0)
		->setCellValue('A1', '序号')
		->setCellValue('B1', '内容');
		
		// 内容
		for ($i = 0, $len = count($data); $i < $len; $i++) {
			$objPHPExcel->getActiveSheet(0)->setCellValue('A' . ($i + 2), $i);
			$objPHPExcel->getActiveSheet(0)->setCellValue('B' . ($i + 2), ' '.$data[$i]['name'], \PHPExcel_Cell_DataType::TYPE_STRING);
		}
		
		// Set active sheet index to the first sheet, so Excel opens this as the first sheet
		$objPHPExcel->setActiveSheetIndex(0);
		
		// 输出
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="' . $filename . '.xls"');
		header('Cache-Control: max-age=0');
		
		$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		$objWriter->save('php://output');
	}
	
	/**
	 * 
	 */
	public function actionScan()
	{
		echo '<pre>';
		
		$namespcace = __NAMESPACE__;
		
		// echo $namespcace;exit;
		
		$arrAction = [ ];
		
		$dirname = dirname ( __FILE__ . '../' );
		$dirs = scandir ( $dirname );
		foreach ( $dirs as $dir )
		{
			if (strpos ( $dir, '.php' ) > 0)
			{
				$className = $namespcace . '\\' . $dir;
				$className = str_replace ( '.php', '', $className );
				$this->getAction ( $className );
			}
		}
		
		print_r ( $this->allActions );
	}
	
	/**
	 * 
	 * @param Object $class
	 * @return void
	 */
	public function getAction($class)
	{
		$class = new \ReflectionClass ( $class );
		$methods = $class->getMethods ( \ReflectionMethod::IS_PUBLIC );
		
		foreach ( $methods as $method )
		{
			
			if (preg_match ( '/^action[A-Z][A-Za-z0-9]+$/', $method->name ))
			{
				$actionInfo['controller'] = $method->class;
				$actionInfo['action'] = $method->name;
				
				$this->allActions [] = $actionInfo;
				
				
			}
		}
	}
	
	public function actionBug()
	{
		//批量生成二维码
		$images = [];
		
		for ($i=1; $i<=10; $i++)
		{
			$images[] = $this->createQrcode($i);
		}
		
		print_r($images);
	}
	
	
	
	private function createQrcode($size)
	{
		$value = 'http://wx.zhishimama.com/source-statistics/index?id=10';
		$errorCorrectionLevel = 'L';
		$matrixPointSize = $size;
	
		$basePath = 'qrcode/';
	
		$time = uniqid();
	
		$filePath = UPLOAD_PATH . $basePath;
		$this->createDir ( $filePath );
		$png_file = $filePath . $time . '.png';
	
		include_once (ROOT_PATH . "/vendor/phpqrcode/phpqrcode.php");
	
		\QRcode::png ( $value, $png_file, $errorCorrectionLevel, $matrixPointSize );
	
		return $basePath . $time . '.png';
	}
	
	/**
	 * 递归创建目录
	 *
	 * @param string $path
	 */
	private function createDir($path)
	{
		if (! file_exists ( $path ))
		{
			$this->createDir ( dirname ( $path ) );
			mkdir ( $path, 0777 );
		}
	}


	public function actionRds()
	{
		$redis=new Concurrency('6f9fd7e836a50ee8f0fjkajsdflj',2,'zsmmcoupon',1485076660,false);
//		$obj=Rds::getConnect();
//		$list_key=$redis->getUniqueKey();
//		$redis->getConnect()->del($list_key);
//		self::dump($redis->getConnect()->lGetRange($list_key,0,-1));die;
		if(!$redis->is_end()){
			$go_result=$redis->run();
			self::dump($redis->getConnect()->SMEMBERS('active_end_d5c90dbc229dccee9046f6491bdf6679'));
			self::dump($go_result);
			die;
		}else{
			echo '活动已经结束';
		}

	}
}

